package com.yuanduan.mplus.view;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextPaint;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.cj.framework.app.BaseActivityWithBundle;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * 行车记录
 */
public class DriveRecordDetailActivity extends BaseActivityWithBundle {
    private MapView mapView;
    private AMap aMap;
    private Polyline polyline;
    private double latitude=29.087382;
    private double longtitude=119.635879;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex callback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            switch (index){
                case 0:

                    break;
                case 1:
                    //解析结果
                    //画上去
                    PolylineOptions options=new PolylineOptions();
                    aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(latitude,longtitude)));
                    LatLng latLng1;
                    LatLng latLng2;
                    LatLng latLng3;
                    LatLng latLng4;
                    LatLng latLng5;
                    LatLng latLng6;
                    LatLng latLng7;
                    LatLng latLng8;
                    options.add(latLng1=new LatLng(latitude,longtitude));
                    options.add(latLng2=new LatLng(latitude+0.001,longtitude+0.001));
                    options.add(latLng3=new LatLng(latitude+0.002,longtitude+0.002));
                    options.add(latLng4=new LatLng(latitude+0.003,longtitude+0.003));
                    options.add(latLng5=new LatLng(latitude+0.004,longtitude+0.004));
                    options.add(latLng6=new LatLng(latitude+0.004,longtitude+0.005));
                    options.add(latLng7=new LatLng(latitude+0.004,longtitude+0.006));
                    options.add(new LatLng(latitude+0.004,longtitude+0.007));
                    options.add(new LatLng(latitude+0.005,longtitude+0.007));
                    options.add(new LatLng(latitude+0.006,longtitude+0.007));
                    options.add(latLng8=new LatLng(latitude+0.007,longtitude+0.007));
                    polyline=aMap.addPolyline(options);
                    MarkerOptions options1=new MarkerOptions();
                    options1.position(latLng2);
                    options1.icon(BitmapDescriptorFactory.fromBitmap(getMybitmap("haha")));
                    Marker marker=aMap.addMarker(options1);
//                    marker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.back_arrow_black));
//                    marker.setSnippet("meme");

                    List<LatLng> lists=new ArrayList<>();
                    lists.add(latLng1);
                    lists.add(latLng2);
                    lists.add(latLng3);
                    lists.add(latLng4);
                    lists.add(latLng5);
                    lists.add(latLng6);
                    lists.add(latLng7);
                    lists.add(latLng8);
                    polyline.setPoints(lists);
                    polyline.setColor(Color.RED);
                    polyline.setWidth(3f);
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };

    private Bitmap getMybitmap(String str){
        Bitmap bitmap1=BitmapDescriptorFactory.fromResource(R.mipmap.back_arrow_black).getBitmap();
        bitmap1=Bitmap.createBitmap(bitmap1,0,0,bitmap1.getWidth(),bitmap1.getHeight());
        Canvas canvas=new Canvas(bitmap1);
        TextPaint paint=new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        paint.setTextSize(22f);
        paint.setColor(Color.BLACK);
        canvas.drawText("haha",17,35,paint);
        return bitmap1;
    }
    @Override
    protected void findId() {
        mapView= (MapView) findViewById(R.id.mapView);
    }

    @Override
    protected void logic() {
        aMap=mapView.getMap();
        //发送请求获取多少公里多少小时
        aMap.moveCamera(CameraUpdateFactory.zoomTo(19));
        iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),callback,0);
        //发送请求获取点坐标
        iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),callback,1);
    }


    @Override
    protected int setLayout() {
        return R.layout.activity_drive_record_detail;
    }


    @Override
    protected void logicWithBundle(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
